<html>
<script src='js/jquery.js'></script>
<script>
	var valid_extension = ["doc", "docx", "pdf", "ppt", "pps", "tiff"]; // "xls", "xlsx", "ods", "csv", "tsv", "txt", "tsb", "sxw", 
	
	chrome.extension.onRequest.addListener(
		function(request, sender, sendResponse) {
			switch (request.type) {
				case 'get_ext':
					// return json object
					var obj = "";
					for (var i = 0; i < valid_extension.length; i++){
						obj	+= valid_extension[i] + ":" + localStorage["ugdv_" + valid_extension[i]] + ",";
					}
					obj += "newTab:" + localStorage["ugdv_newTab"];
					sendResponse(obj);
					break;
				case "open_win":
					chrome.windows.create({url:request.url});
					sendResponse();
					break;
				case "open_tab":
					chrome.tabs.create({url:request.url});
					sendResponse();
					break;
				case 'set':
					localStorage[request.key] = request.value;
					break;
				case 'get':
					sendResponse(localStorage[request.key]);
					break;
				case 'logged_in':
					// turn off caching
					$.get("https://docs.google.com/DocAction", {action: "updoc", classic: "true"}, function(e, s){
						var to = $(e).find("input[name=security_token]").val();
						to ? sendResponse(true) : sendResponse(false);
					});
					break;
				case 'send_to_docs':
					var regexp = /(\w|[-.])+$/;
					str = request.url;
					a = regexp.exec(str);
					var file_name = a[0];
					$.get("https://docs.google.com/DocAction", {action: "updoc", classic: "true"}, function(e, s){
						alert($(e).find("body").html());
						var token = $(e).find("input[name=security_token]").val();
						if (token){
							$.post("https://docs.google.com/DocAction?action=updoc", 
								{ 
									security_token: token, 
									formsubmitted: "true", 
									uploadURL: request.url, 
									DocName: file_name, 
									saveChanges: "Upload File" 
								}, 
								function(d, t){
									sendResponse(t == "success");
								}
							);
						}else{
							sendResponse(false);
						}
					}, "html");
					break;
				default:
					sendResponse();
					break;
			}
		}
	); 
</script>
</html>